﻿using System;
using System.IO;
using System.Data;
using System.Windows;
using System.Collections;
using System.Windows.Input;
using System.ComponentModel;
using System.Windows.Controls;
using Sinacor.Infra.UI.Common;
using Sinacor.Infra.UI.Controls;
using Sinacor.UI.Bovespa.Corretagem.Model;
using Sinacor.UI.Bovespa.Corretagem.Presenter;
using Sinacor.Infra.UI.Controls.SmartClient;
using System.Collections.Generic;
using Sinacor.Infra.UI.Controls.SmartClient.Validation;
using PSE.Framework.UI.Resources;
using Sinacor.UI.Bovespa.Clientes.View.SmartClient;

namespace Sinacor.UI.Bovespa.Corretagem.View.SmartClient
{
    /// <summary>
    /// Interaction logic for CadastroDesconto.xaml
    /// </summary>
    public partial class CadastroDescontos : SinacorWindow,ICadastroDesconto
    {
        #region Campos

        public Presenter.CadastroDesconto PresenterCadastroDesconto { get; set; }

        #endregion

        #region Construtores

        public CadastroDescontos()
        {
            
             InitializeComponent();
            PresenterCadastroDesconto = new Presenter.CadastroDesconto(this);

            grdOperacao.NewItemType = typeof(Model.DescontoCliente);
            grdAfterMarket.NewItemType = typeof(Model.Desconto);

            DataContext = PresenterCadastroDesconto;

            AfterInitialize += new RoutedEventHandler(Inicializar);
           
        }

        #endregion

        
        #region Eventos

        private void Inicializar(object sender, RoutedEventArgs e)
        {
            ShowUserWaitingProcess();

            PresenterCadastroDesconto.Inicializar();

            HideUserWaitingProcess();
        }

        private void grdAfterMarket_CancelingInsert(object sender, CancelItemEventArgs e)
        {

        }

        private void grdAfterMarket_Deleting(object sender, CancelItemsEventArgs e)
        {
            if (grdAfterMarket.SelectedItem != null)
            {
                this.ClearServiceErrors(); 
                this.ShowUserWaitingProcess();
                e.Cancel = !PresenterCadastroDesconto.ExcluirDesconto((Model.Desconto)grdAfterMarket.SelectedItem);
                this.HideUserWaitingProcess();
                if (!e.Cancel)
                    grdAfterMarket.SelectedIndex = -1; 
                   
            }
        }

        private void grdAfterMarket_EndingInsert(object sender, CancelItemEventArgs e)
        {
            if (e.OriginalSource is BaseRow)
            {
                this.ClearServiceErrors(); 
                this.ShowUserWaitingProcess();
                e.Cancel = !PresenterCadastroDesconto.IncluirDesconto((Model.Desconto)grdAfterMarket.EditItem);
                this.HideUserWaitingProcess();
            }
        }

        private void grdOperacao_EndingInsert(object sender, CancelItemEventArgs e)
        {
            if (e.OriginalSource is BaseRow)
            {
                this.ClearServiceErrors(); 
                this.ShowUserWaitingProcess();
                SinacorDecimalTextBox textPercentual= (SinacorDecimalTextBox)ControlHelper.FindChild(grdOperacao.GetInsertionRow(), "textPercentualAcrescimoDescontoCliente");
                if (!String.IsNullOrEmpty(textPercentual.Text) && Convert.ToDecimal(textPercentual.Text) != textPercentual.Value)
                    textPercentual.Value = Convert.ToDecimal(textPercentual.Text); 
                e.Cancel = !PresenterCadastroDesconto.IncluirDescontoCliente((Model.DescontoCliente)grdOperacao.EditItem);

                this.HideUserWaitingProcess();
            }
        }

        private void grdOperacao_EndingUpdate(object sender, CancelItemEventArgs e)
        {
            if (e.OriginalSource is BaseRow)
            {
                this.ShowUserWaitingProcess(); 
                this.ClearServiceErrors();
                SinacorDecimalTextBox textPercentual = (SinacorDecimalTextBox)ControlHelper.FindChild(grdOperacao.GetEditRow(), "textPercentualAcrescimoDescontoCliente");
                if (!String.IsNullOrEmpty(textPercentual.Text) && Convert.ToDecimal(textPercentual.Text) != textPercentual.Value)
                    textPercentual.Value = Convert.ToDecimal(textPercentual.Text); 
                e.Cancel = !PresenterCadastroDesconto.AlterarDescontoCliente((Model.DescontoCliente)grdOperacao.EditItem);
                this.HideUserWaitingProcess(); 
            }
        }

        private void grdOperacao_Deleting(object sender, CancelItemsEventArgs e)
        {
            if (grdOperacao.SelectedItem != null)
            {
                this.ShowUserWaitingProcess(); 
                this.ClearServiceErrors(); 
                e.Cancel = !PresenterCadastroDesconto.ExcluirDescontoCliente((Model.DescontoCliente)grdOperacao.SelectedItem);
                this.HideUserWaitingProcess(); 
            }
        }


        private void btnPesquisa_OnBufferedClick(object sender, RoutedEventArgs e, int clickCount)
        {
            this.cancelarEdicao();
            this.ClearServiceErrors();
            this.ShowUserWaitingProcess(); 
            PresenterCadastroDesconto.ConsultarDescontosClientes();
            this.HideUserWaitingProcess(); 
        }

        private void btnNovaPesquisa_OnBufferedClick(object sender, RoutedEventArgs e, int clickCount)
        {
            ShowUserWaitingProcess();
            this.cancelarEdicao();
            this.ClearServiceErrors();
            this.PresenterCadastroDesconto.AtualizarNovaPesquisa();
            HideUserWaitingProcess(); 
        }


        private void btnPesquisarCliente_OnBufferedClick(object sender, RoutedEventArgs e, int clickCount)
        {
            this.cancelarEdicao();
            this.ClearServiceErrors();
            this.PresenterCadastroDesconto.AbrirPesquisaCliente();    
        }

        private void grdAfterMarket_BeingInserted(object sender, CancelItemEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                this.ClearServiceErrors();
                Model.Desconto RegAtual = (Model.Desconto)this.grdAfterMarket.EditItem;
                RegAtual.Periodo = new Periodo();
            }
        }

        private void grdAfterMarket_InsertEnded(object sender, ItemEventArgs e)
        {
            if (e.OriginalSource is BaseRow)
            {
                this.ClearServiceErrors();
                this.PresenterCadastroDesconto.ConsultarDescontosAfterMarket();
                this.AtualizarDescontosAfterMarket();
            }
        }

        private void grdOperacao_BeingInserted(object sender, CancelItemEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
                this.ClearServiceErrors();
                this.PresenterCadastroDesconto.Mensagem = 0; 
            }
        }

        private void grdOperacao_BeingUpdated(object sender, CancelItemEventArgs e)
        {
            if (e.OriginalSource is SinacorDataGrid)
            {
              this.ClearServiceErrors();
              this.PresenterCadastroDesconto.Mensagem = 0;
              this.PresenterCadastroDesconto.ApelidoAntigo = string.Empty; 
            }
        }
        private void txtApelidoCliente_LotFocus(object sender, RoutedEventArgs e)
        {
            this.ShowUserWaitingProcess();
            if (this.grdOperacao.EditItem != null)
            {
                if (!String.IsNullOrEmpty(((Model.DescontoCliente)this.grdOperacao.EditItem).ApelidoCliente))
                {
                    this.ClearServiceErrors();
                    Model.DescontoCliente RegAtual = (Model.DescontoCliente)this.grdOperacao.EditItem;
                    PresenterCadastroDesconto.VerificarApelidoCliente(ref RegAtual);
                }
            }
            this.HideUserWaitingProcess(); 
        }
        private void textApelidoCliente_LostFocus(object sender, RoutedEventArgs e)
        {


            object[] parm = { e };
            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new RoutedEventHandler(textApelidoClienteLostFocus), sender, parm);
        }

        private void textApelidoClienteLostFocus(object sender, RoutedEventArgs e)
        {

            this.ShowUserWaitingProcess();
            if (!string.IsNullOrEmpty(textApelidoCliente.Text))
            {

                this.ClearServiceErrors();

                Model.DescontoCliente RegAtual = new DescontoCliente();

                RegAtual.ApelidoCliente = textApelidoCliente.Text;

                PresenterCadastroDesconto.VerificarApelidoCliente(ref RegAtual);

            }
            this.HideUserWaitingProcess(); 
        }
        private void cancelarEdicao()
        {
            if (grdOperacao.EditIndex == -1)
                grdOperacao.CancelInsert();

            if (grdOperacao.EditIndex > -1)
                grdOperacao.CancelUpdate();

            if (grdAfterMarket.EditIndex == -1)
                grdAfterMarket.CancelInsert();

            if (grdAfterMarket.SelectedIndex > -1)
                grdAfterMarket.CancelUpdate();

        }
        #endregion 

        #region ICadastroDesconto Implementação



        public void AbrirPesquisaCliente()
        {
            this.ShowUserWaitingProcess();
            PesquisaClientes pesquisaCliente = new PesquisaClientes();
            pesquisaCliente.Owner = this;
            pesquisaCliente.SetMenuHeader("Bovespa.Clientes.Label.PesquisaClientesMenuHeader");
            pesquisaCliente.CarregaParametros(this.PresenterCadastroDesconto.JanelaCadatroDesconto, this.PresenterCadastroDesconto.FiltroPesquisaCliente);
            pesquisaCliente.ShowDialog();
            this.HideUserWaitingProcess();
            PresenterCadastroDesconto.RetornoPesquisaCliente(pesquisaCliente.PesquisaClienteModel);
        }

        public void AtualizarPesquisa()
        {
            cmbTipoOperacao.UpdateTarget(SinacorComboBox.ItemsSourceProperty); 
            textApelidoCliente.UpdateTarget(SinacorTextBox.TextProperty);
            cmbTipoOperacao.UpdateTarget(SinacorComboBox.SelectedValueProperty); 
        }

        public void  AtualizarDescontoCliente()
        {
 	       grdOperacao.UpdateTarget (SinacorDataGrid.ItemsSourceProperty) ; 
        }

        public void  AtualizarDescontosAfterMarket()
        {
 	        grdAfterMarket.UpdateTarget(SinacorDataGrid.ItemsSourceProperty); 
        }

        public void AtualizarNovaPesquisa()
        {
            cmbTipoOperacao.UpdateTarget(SinacorComboBox.SelectedValueProperty);
            textApelidoCliente.UpdateTarget(SinacorTextBox.TextProperty); 
            
        }


        public bool  ConfirmarExclusaoDesconto()
        {
 	        return  (SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.Message.ConfirmaExclusaoDesconto"), SinacorMessageBoxButton.YesNo,SinacorMessageBoxImage.Question) == SinacorMessageBoxResult.Yes); 
        }

        public bool  ConfirmarPermiteAjuste()
        {
 	        return  (SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.ExceptionMessage.DescontoDuplicado"), SinacorMessageBoxButton.YesNo,SinacorMessageBoxImage.Question) == SinacorMessageBoxResult.Yes); 
        }

        public void  DesabilitarAbaAfterMarket()
        {
            // Task  :  5969
            // Autor :  Apsilva
            // Data  :  31/03/2009
            tabControle.Items.Remove(tabControle.SelectedItem); 
            tabControle.UpdateLayout();  
           
        }

        public void  DesabilitarAbaPorOperacao()
        {
            // Task  :  5969
            // Autor :  Apsilva
            // Data  :  31/03/2009
 	        tbiOperacao.Visibility = Visibility.Hidden;
            tabControle.SelectedIndex = 0;
            
        }

        public void  InformarAllInObrigatorio()
        {
 	        SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.ExceptionMessage.AllInObrigatorio"), SinacorMessageBoxButton.Ok,SinacorMessageBoxImage.Information);
        }

        public void  InformarApelidoObrigatorio()
        {
 	        SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.ExceptionMessage.ApelidoClienteObrigatorio"), SinacorMessageBoxButton.Ok,SinacorMessageBoxImage.Information); 
        }

        public void  InformarClienteInexistente()
        {
 	        SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.ExceptionMessage.ClienteInexistente"), SinacorMessageBoxButton.Ok ,SinacorMessageBoxImage.Information);        

        }

        public void  InformarClienteInvalido()
        {
 	        SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.ExceptionMessage.ClienteInvalido"), SinacorMessageBoxButton.Ok,SinacorMessageBoxImage.Information);        
        }

        public void  InformarCodigoClienteObrigatorio()
        {
 	        SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.ExceptionMessage.CodigoClienteObrigatorio"), SinacorMessageBoxButton.Ok,SinacorMessageBoxImage.Information);
        }

        public void  InformarConflitoSemAjuste()
        {
           SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.ExceptionMessage.DescontoConflitoData"), SinacorMessageBoxButton.Ok,SinacorMessageBoxImage.Information);
        }

        public void  InformarDataFinalInvalida()
        {          
            SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.ExceptionMessage.DataFinalInvalida"), SinacorMessageBoxButton.Ok,SinacorMessageBoxImage.Information);
        }

        public void InformarDataFinalObrigatoria()
        {
            SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.ExceptionMessage.DataFinalDescontoObrigatoria"), SinacorMessageBoxButton.Ok, SinacorMessageBoxImage.Information); 
        }


        public void InformarDataInicialObrigatoria()
        {
            SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.ExceptionMessage.DataInicialDescontoObrigatoria"), SinacorMessageBoxButton.Ok, SinacorMessageBoxImage.Information);
        }
        public void InformarDescontoAlteradoSucesso()
        {
 	        SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.Message.AlteracaoRealizadaSucesso"), SinacorMessageBoxButton.Ok,SinacorMessageBoxImage.Information);

        }

        public void  InformarDescontoExcluidoSucesso()
        {
 	        SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.Message.ExclusaoRealizadaSucesso"), SinacorMessageBoxButton.Ok,SinacorMessageBoxImage.Information);

        }

        public void  InformarDescontoIncluidoSucesso()
        {
 	        SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.Message.InclusaoRealizadaSucesso"), SinacorMessageBoxButton.Ok,SinacorMessageBoxImage.Information);

        }

        public void  InformarDescontoMaiorCem()
        {
 	        SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.ExceptionMessage.DescontoMaiorQueCem"), SinacorMessageBoxButton.Ok,SinacorMessageBoxImage.Information);

        }

        public void  InformarDescontoObrigatorio()
        {
 	        SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.ExceptionMessage.PercentualDescontoObrigatorio"), SinacorMessageBoxButton.Ok,SinacorMessageBoxImage.Information);

        }

        public void InoformarTipoOperacaoDesconto()
        {
            SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.ExceptionMessage.TipoOperacaoDescontoObrigatorio"), SinacorMessageBoxButton.Ok, SinacorMessageBoxImage.Information);
        }
        public void  InformarSelecioneDesconto()
        {
            SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.Message.SelecioneDesconto"), SinacorMessageBoxButton.Ok,SinacorMessageBoxImage.Information);        
        }

        public void PesquisaSemDados()
        {
            SinacorMessageBox.Show((string)ResourceProviderManager.GetResourceProvider().TryGetResource("Bovespa.Corretagem.Message.PesquisaSemRetorno"), SinacorMessageBoxButton.Ok, SinacorMessageBoxImage.Information);        
        }

#endregion

        

    }
}
